package com.springcloud.eshop.aggrdata.rabbitmq.strategy;

import com.springcloud.eshop.aggrdata.rabbitmq.factory.AggrDataChangeHandlerFactory;
import com.springcloud.eshop.enums.DataTypeEnum;
import com.springcloud.eshop.message.AggrDataChangeMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;

/**
 * @Desc:
 * @Date: 2020-07-02
 * @Version:v1.0
 */
@Service
public class ProductPropertyChangeStrategy extends BaseAggrDataChangeHandler implements InitializingBean {

    @Autowired
    private JedisPool jedisPool;

    @Override
    public boolean handler(AggrDataChangeMessage aggrDataChangeMessage) {
        long id = aggrDataChangeMessage.getId();
        String result = jedisPool.getResource().get(getRedisKey(id));
        if (StringUtils.isNotEmpty(result)) {
            jedisPool.getResource().set("dim_product_property_" + id, result);
        } else {
            jedisPool.getResource().del("dim_product_property_" + id);
        }
        return true;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        AggrDataChangeHandlerFactory.register(DataTypeEnum.PRODUCT_PROPERTY.getType(), this);
    }

    @Override
    public String getRedisKey(long id) {
        return "product_property_detail_" + id;
    }

}
